package com.tzinfo.tzt.activity.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.CarLifeDetailActivity;
import com.tzinfo.tzt.util.Resultmap;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2014/11/7.
 */
public class FragmentHomeListviewAdapter3 extends BaseAdapter{

    private Context context;
    private List<Resultmap> data;
     ImageLoader imageLoader;

    public FragmentHomeListviewAdapter3(Context context, List<Resultmap> data) {
        this.context = context;
        this.data = data;
        imageLoader = ImageLoader.getInstance();

    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_item_car_life,parent,false);
            viewHolder.tv_subject =(TextView)convertView.findViewById(R.id.item_car_life_tv_subject);
            viewHolder.tv_views =(TextView)convertView.findViewById(R.id.item_car_life_tv_views);
            viewHolder.tv_dateline=(TextView)convertView.findViewById(R.id.item_car_life_tv_dateline);
            viewHolder.tv_replies=(TextView)convertView.findViewById(R.id.item_car_life_tv_replies);
            viewHolder.iv_attachment=(ImageView)convertView.findViewById(R.id.item_car_life_iv_attachment);
            viewHolder.ll=(LinearLayout)convertView.findViewById(R.id.item_car_life_ll);

            convertView.setTag(viewHolder);
        }
        else {
               viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tv_subject.setText(data.get(position).getSubject().toString());
        viewHolder.tv_views.setText(String.valueOf(data.get(position).getViews()));
        viewHolder.tv_dateline.setText(data.get(position).getDateline().toString().split(" ")[0]);
        viewHolder.tv_replies.setText(String.valueOf(data.get(position).getReplies()));
        viewHolder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CarLifeDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("tid", data.get(position).getTid());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        if (data.get(position).getAttachment().size()!=0)
        imageLoader.displayImage(data.get(position).getAttachment().get(0).getAttachment(),viewHolder.iv_attachment);

        return  convertView;
    }
    class ViewHolder{
        TextView tv_subject;
        TextView tv_views;
        TextView tv_dateline;
        TextView tv_replies;
        ImageView iv_attachment;
        LinearLayout ll;
    }
}
